using Android.App;
using Android.Graphics;
using Android.Graphics.Drawables;
using Java.Lang;
using System.Threading.Tasks;
using ZSoft.PushServices.Client.NetFramework;
using ZSoft.PushServices.Net.DTO;
using static Utils.Utilities;

namespace ZSoft.PushServices.Android
{
    public static class NotificationBuilderExtensions
    {
        public static Notification.Builder AddTitle(this Notification.Builder source, string title)
        {
            return source.SetContentTitle(title);
        }

        public static Notification.Builder AddMessage(this Notification.Builder source, string text)
        {
            return source.SetContentTitle(text);
        }

        public async static Task<Notification.Builder> AddSmalIcon(this Notification.Builder source, IconDTO dto)
        {
            return source.SetSmallIcon(await GetSmallIcon(dto));
        }

        private static async Task<Icon> GetSmallIcon(IconDTO smallIcon)
        {
            return Icon.CreateWithBitmap(CreateBitMap(await GetResponse(smallIcon.Url)));
        }

        public async static Task<Notification.Builder> AddLargeIcon(this Notification.Builder source, IconDTO largeIcon)
        {
            return source.SetLargeIcon(await GetLargeIconFromDTO(largeIcon));
        }

        private static async Task<Bitmap> GetLargeIconFromDTO(IconDTO largeIcon)
        {
            return CreateBitMap(await GetResponse(largeIcon.Url));
        }

        private static Bitmap CreateBitMap(byte[] content)
        {
            try
            {
                return BitmapFactory.DecodeByteArray(content, 0, content.Length);
            }
            catch (Exception ex)
            {
                PushServiceApplication.ApplicationError(ex);
                return null;
            }
            catch(System.Exception ex)
            {
                PushServiceApplication.ApplicationError(ex);
                return null;
            }
        }
    }
}